package com.wanghua.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.product.entity.BrandCategoryRelationEntity;
import com.wanghua.mall.product.service.BrandCategoryRelationService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌&三级分类关联
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-23 16:10:06
 */
@RestController
@RequestMapping("/product/brandcategoryrelation")
public class BrandCategoryRelationController {

    @Resource
    private BrandCategoryRelationService brandCategoryRelationService;

    //根据品牌id获取品牌关联的分类
    @GetMapping("/listByBrandId")
    public R listByBrandId(@RequestParam("brandId") Long brandId) {
        List<BrandCategoryRelationEntity> list = brandCategoryRelationService.listByBrandId(brandId);
        return R.ok().put("items", list);
    }

    //根据分类id获取分类关联的品牌
    @GetMapping("/listByCategoryId")
    public R listByCategoryId(@RequestParam("categoryId") Long categoryId) {
        List<BrandCategoryRelationEntity> list = brandCategoryRelationService.listByCategoryId(categoryId);
        return R.ok().put("items", list);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandCategoryRelationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        BrandCategoryRelationEntity brandCategoryRelation = brandCategoryRelationService.getById(id);
        return R.ok().put("brandCategoryRelation", brandCategoryRelation);
    }

    /**
     * 新增品牌与分类关联关系,传递过来的参数是品牌id和分类id
     */
    @PostMapping("/save")
    public R save(@RequestBody BrandCategoryRelationEntity brandCategoryRelation) {
        brandCategoryRelationService.saveDetail(brandCategoryRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody BrandCategoryRelationEntity brandCategoryRelation) {
        brandCategoryRelationService.updateById(brandCategoryRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        brandCategoryRelationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
